package com.jeegit.ecms.service.impl;

import java.util.List;
import com.jeegit.common.utils.DateUtils;
import com.jeegit.common.utils.IdUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jeegit.ecms.mapper.CmsArticleDataMapper;
import com.jeegit.ecms.mapper.CmsArticleMapper;
import com.jeegit.ecms.domain.CmsArticle;
import com.jeegit.ecms.domain.CmsArticleData;
import com.jeegit.ecms.service.ICmsArticleService;
import com.jeegit.common.core.text.Convert;

/**
 * 文章Service业务层处理
 * 
 * @author jeegit
 * @date 2022-06-30
 */
@Service
public class CmsArticleServiceImpl implements ICmsArticleService 
{
    @Autowired
    private CmsArticleMapper cmsArticleMapper;
    
    @Autowired
    private CmsArticleDataMapper cmsArticleDataMapper;

    /**
     * 查询文章
     * 
     * @param id 文章主键
     * @return 文章
     */
    @Override
    public CmsArticle selectCmsArticleById(Long id)
    {
	CmsArticle entity= cmsArticleMapper.selectCmsArticleById(id);
	if (entity != null && entity.getId()>0l) {
		entity.setCmsArticleData(cmsArticleDataMapper.selectCmsArticleDataById(id));
	}
        return entity;
    }

    /**
     * 查询文章列表
     * 
     * @param cmsArticle 文章
     * @return 文章
     */
    @Override
    public List<CmsArticle> selectCmsArticleList(CmsArticle cmsArticle)
    {
        return cmsArticleMapper.selectCmsArticleList(cmsArticle);
    }

    /**
     * 新增文章
     * 
     * @param cmsArticle 文章
     * @return 结果
     */
    @Override
    public int insertCmsArticle(CmsArticle cmsArticle)
    {
    	cmsArticle.setId(IdUtils.getInstance(3l).nextId());
    	cmsArticle.preInsert(cmsArticle);
    	cmsArticle.getCmsArticleData().setId(cmsArticle.getId());
    	cmsArticle.getCmsArticleData().preUpdate(cmsArticle.getCmsArticleData());
    	int resultCount=cmsArticleMapper.insertCmsArticle(cmsArticle);
    	cmsArticleDataMapper.insertCmsArticleData(cmsArticle.getCmsArticleData());
    	return resultCount;
    }

    /**
     * 修改文章
     * 
     * @param cmsArticle 文章
     * @return 结果
     */
    @Override
    public int updateCmsArticle(CmsArticle cmsArticle)
    {
    	cmsArticle.preUpdate(cmsArticle);
    	cmsArticle.getCmsArticleData().preUpdate(cmsArticle.getCmsArticleData());
    	cmsArticle.getCmsArticleData().setId(cmsArticle.getId());
    	int resultCount=cmsArticleMapper.updateCmsArticle(cmsArticle);
    	cmsArticleDataMapper.updateCmsArticleData(cmsArticle.getCmsArticleData());
        return resultCount;
    }

    /**
     * 批量删除文章
     * 
     * @param ids 需要删除的文章主键
     * @return 结果
     */
    @Override
    public int deleteCmsArticleByIds(String ids)
    {
        return cmsArticleMapper.deleteCmsArticleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除文章信息
     * 
     * @param id 文章主键
     * @return 结果
     */
    @Override
    public int deleteCmsArticleById(Long id)
    {
        return cmsArticleMapper.deleteCmsArticleById(id);
    }
}
